﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Data.Sql;
using System.Data.SqlClient;
using Newtonsoft.Json;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;
using System.Collections;
using System.Net;
using System.Xml.Linq;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            //G.Service.BLL.IResult result = null;
            //result = G.Service.BLL.UserBLL.Register("ghwghw4@163.com", "ghwghw", "大哥豪爽");
            HttpWebRequest sendNotificationRequest = (HttpWebRequest)WebRequest.Create("http://db3.notify.live.net/throttledthirdparty/01.00/AAEOs-WQ0J-xTbzOWDT8b0ZRAgAAAAADCgAAAAQUZm52OkJCMjg1QTg1QkZDMkUxREQ");
            sendNotificationRequest.Method = "POST";
            sendNotificationRequest.ContentType = "text/xml; charset=utf-8";
            sendNotificationRequest.Headers.Add("X-WindowsPhone-Target", "toast");
            sendNotificationRequest.Headers.Add("X-NotificationClass", "2");
            string toastMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<wp:Notification xmlns:wp=\"WPNotification\">" +
    "<wp:Toast>" +
        "<wp:Text1>1ds</wp:Text1>" +
        "<wp:Text2>2dsd</wp:Text2>" +
    "</wp:Toast>" +
"</wp:Notification>";
            XElement xe = XElement.Parse(toastMessage);
            var aa = xe.Elements().FirstOrDefault();
            byte[] buffer = Encoding.UTF8.GetBytes(xe.ToString());
            sendNotificationRequest.ContentLength = buffer.Length;


            using (Stream stream = sendNotificationRequest.GetRequestStream())
            {
                stream.Write(buffer, 0, buffer.Length);
            }

            //stream.Close();
            var response = sendNotificationRequest.GetResponse();
            using (System.IO.StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                var str = reader.ReadToEnd();
                if (str == null)
                {

                }
            }

        }

        private static void Write(object target, Type type, JsonWriter jsonWriter)
        {

            if (type.IsValueType || type == typeof(string))
            {
                jsonWriter.WriteValue(target);
            }
            else if (type.GetInterface("IEnumerable") != null)
            {
                IEnumerable ar = target as IEnumerable;
                jsonWriter.WriteStartArray();
                foreach (var item in ar)
                {
                    Write(item, item.GetType(), jsonWriter);
                }
                jsonWriter.WriteEndArray();
            }
            else
            {
                jsonWriter.WriteStartObject();
                IEnumerable<PropertyInfo> ps = target.GetType().GetProperties();
                if (target.GetType().IsSubclassOf(typeof(System.Data.Objects.DataClasses.EntityObject)))
                    ps = ps.Where(c => (c.Name != "EntityKey" && c.Name != "EntityState"));
                foreach (var p in ps)
                {
                    var att = Attribute.GetCustomAttribute(p, typeof(DataMemberAttribute));
                    if (att == null)
                        continue;
                    var value = p.GetValue(target, null);
                    if (value == null)
                        continue;
                    jsonWriter.WritePropertyName(p.Name);
                    Write(value, p.PropertyType, jsonWriter);
                }
                //Write(target, jsonWriter);
                jsonWriter.WriteEndObject();
            }
        }
    }
}
